Skip to content

SliceValueTransformationProxyModel

Base classes

Name Children Inherits
SliceIdentityProxyModel
prettyqt.itemmodels.proxies.sliceidentityproxymodel
Base model for slice proxy models.

â‹” Inheritance diagram

graph TD
  3157645503["itemmodels.SliceValueTransformationProxyModel"]
  4214724289["itemmodels.SliceIdentityProxyModel"]
  527136029["core.IdentityProxyModel"]
  2431222121["core.AbstractProxyModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  3668892142["QtCore.QIdentityProxyModel"]
  3989152870["QtCore.QAbstractProxyModel"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  4214724289 --> 3157645503
  527136029 --> 4214724289
  2431222121 --> 527136029
  3882849576 --> 2431222121
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  3668892142 --> 527136029
  3989152870 --> 3668892142
  1898060022 --> 3989152870
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: SliceIdentityProxyModel

A proxy model which transforms cell contents based on a Callable.

Example:

model = MyModel()
table = widgets.TableView()
table.set_model(model)
table.proxifier[::2, 2:].modify(xyz)
table.show()

or

indexer = (slice(None, None, 2), slice(2, None))
proxy = itemmodels.SliceValueTransformationProxyModel(indexer=indexer)
proxy.set_source_model(model)
proxy.add_transformer(lambda x: x + "something", selector=lambda x: "abc" in x)
table.set_model(proxy)
table.show()
Source code in prettyqt/itemmodels/proxies/slicevaluetransformationproxymodel.py
class SliceValueTransformationProxyModel(itemmodels.SliceIdentityProxyModel):
    """A proxy model which transforms cell contents based on a Callable.

    ### Example:
    ```py
    model = MyModel()
    table = widgets.TableView()
    table.set_model(model)
    table.proxifier[::2, 2:].modify(xyz)
    table.show()
    ```

    or

    ```py
    indexer = (slice(None, None, 2), slice(2, None))
    proxy = itemmodels.SliceValueTransformationProxyModel(indexer=indexer)
    proxy.set_source_model(model)
    proxy.add_transformer(lambda x: x + "something", selector=lambda x: "abc" in x)
    table.set_model(proxy)
    table.show()
    ```
    """

    ID = "value_transformation"

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._transformers: list[Transformer] = []

    def clear(self):
        """Clear all transformers."""
        self._transformers = []

    def add_transformer(
        self,
        fn: Callable[[Any], Any],
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
        selector: Callable[[Any], bool] | None = None,
        selector_role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        """Add a transformer for given role.

        If a selector callable is given, the transformer will only be applied if the
        selector returns True.
        The selector receives the content of given data role as an argument.

        Arguments:
            fn: Callable to transform data of given role
            role: Data role to transform
            selector: Callable to filter the indexes which should be transformed
            selector_role: Role to use for the selector callable
        """
        tr = Transformer(
            fn=fn,
            role=role,
            selector=selector,
            selector_role=selector_role,
        )
        self._transformers.append(tr)

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        val = super().data(index, role)
        if not self.indexer_contains(index):
            return val
        for t in self._transformers:
            if t.role == role:
                selector_val = super().data(index, t.selector_role)
                if t.selector is None or t.selector(selector_val):
                    val = t.fn(selector_val)
        return val

add_transformer(fn: Callable[[Any], Any], role: constants.ItemDataRole = constants.DISPLAY_ROLE, selector: Callable[[Any], bool] | None = None, selector_role: constants.ItemDataRole = constants.DISPLAY_ROLE)

Add a transformer for given role.

If a selector callable is given, the transformer will only be applied if the selector returns True. The selector receives the content of given data role as an argument.

Parameters:

Name Type Description Default
fn Callable[[Any], Any]

Callable to transform data of given role

required
role ItemDataRole

Data role to transform

DISPLAY_ROLE
selector Callable[[Any], bool] | None

Callable to filter the indexes which should be transformed

None
selector_role ItemDataRole

Role to use for the selector callable

DISPLAY_ROLE
Source code in prettyqt/itemmodels/proxies/slicevaluetransformationproxymodel.py
def add_transformer(
    self,
    fn: Callable[[Any], Any],
    role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    selector: Callable[[Any], bool] | None = None,
    selector_role: constants.ItemDataRole = constants.DISPLAY_ROLE,
):
    """Add a transformer for given role.

    If a selector callable is given, the transformer will only be applied if the
    selector returns True.
    The selector receives the content of given data role as an argument.

    Arguments:
        fn: Callable to transform data of given role
        role: Data role to transform
        selector: Callable to filter the indexes which should be transformed
        selector_role: Role to use for the selector callable
    """
    tr = Transformer(
        fn=fn,
        role=role,
        selector=selector,
        selector_role=selector_role,
    )
    self._transformers.append(tr)

clear()

Clear all transformers.

Source code in prettyqt/itemmodels/proxies/slicevaluetransformationproxymodel.py
def clear(self):
    """Clear all transformers."""
    self._transformers = []

:fa-brands-github: Show source on GitHub